package com.dianpingmedia.simulate.mapper;

import com.dianpingmedia.common.utils.JsonTypeHandler;
import com.dianpingmedia.simulate.model.*;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

@Mapper
public interface SimulateMapper {


    @Insert("INSERT INTO material(url,name,md5,duration,type,materialType,width,height,notices,dspIds) VALUES(#{material.url},#{material.name},#{material.md5},#{material.duration},#{material.type},#{material.materialType},#{material.width},#{material.height},#{material.notices,jdbcType=VARCHAR,typeHandler=com.dianpingmedia.common.utils.JsonTypeHandler},#{material.dspIds,jdbcType=VARCHAR,typeHandler=com.dianpingmedia.common.utils.JsonTypeHandler})")
    @Options(useGeneratedKeys = true, keyProperty = "material.id", keyColumn = "id")
    Integer uploadMaterial(@Param("material") SimulateUploadEntity.Request request);


    @Select("<script> \n" +
            "SELECT * FROM material WHERE 1>0 " +
            "<if test='queryArgs.id!=null'> AND id=#{queryArgs.id} </if> " +
            "<if test='queryArgs.md5!=null'> AND md5=#{queryArgs.md5} </if> " +
            "<if test='queryArgs.type!=null'> AND type=#{queryArgs.type} </if> " +
            "<if test='queryArgs.materialType!=null'> AND materialType=#{queryArgs.materialType} </if> " +
            "<if test='queryArgs.dspId!=null'> AND(dspIds is null OR dspIds LIKE CONCAT('%',#{queryArgs.dspId},'%'))</if>" +
            "</script>")
    @Results(value = {
            @Result(property = "dspIds", column = "dspIds", typeHandler = JsonTypeHandler.class, jdbcType = JdbcType.VARCHAR, javaType = Integer[].class),
            @Result(property = "notices", column = "notices", typeHandler = JsonTypeHandler.class, jdbcType = JdbcType.VARCHAR, javaType = SimulateUploadEntity.Request.Notice[].class)
    })
    List<SimulateUploadEntity.Request> queryMaterial(@Param("queryArgs") SimulateQueryEntity queryArgs);


    @Select("<script> \n" + "SELECT * FROM material WHERE (dspIds is null OR dspIds LIKE CONCAT('%',#{dspId},'%')) AND type=0 <if test= 'materialType !=0'> AND materialType=#{materialType} </if> order by rand() limit 1 </script>")
    @Results(value = {
            @Result(property = "dspIds", column = "dspIds", typeHandler = JsonTypeHandler.class, jdbcType = JdbcType.VARCHAR, javaType = Integer[].class),
            @Result(property = "notices", column = "notices", typeHandler = JsonTypeHandler.class, jdbcType = JdbcType.VARCHAR, javaType = SimulateUploadEntity.Request.Notice[].class)
    })
    SimulateUploadEntity.Request queryRandomMaterial(@Param("materialType") Integer materialType, @Param("dspId") Integer dspId);


    Integer recordPlan(@Param("request") SimulatePlanEntity.Request request);


    List<DPMaterialEntity> queryMaterialForIds(@Param("materialIds") List<String> materialIds);


    @Insert({"<script> INSERT IGNORE INTO impress(reqId,type,adId," +
            "code,screenId,duration,issuedTime,dspId,url) " +
            "VALUES " +
            "<foreach collection=\"simulateImpressArgs\" item=\"item\" index=\"index\"  separator=\",\"> " +
            "(#{item.id},#{item.type},#{item.adId}," +
            "#{item.code},#{item.screenId},#{item.duration},DATE_FORMAT(#{item.issuedTimeStr},'%Y-%m-%d %H:%i:%s'),#{item.dspId},#{item.url})" +
            "</foreach> </script>"})
    void recordImpress(@Param("simulateImpressArgs") List<SimulateImpressArg> simulateImpressArgs);


    @Insert({"<script> INSERT INTO notes(type,uri,request," +
            "response,returnCode,occurredTime) " +
            "VALUES " +
            "<foreach collection=\"simulateNotes\" item=\"item\" index=\"index\"  separator=\",\"> " +
            "(#{item.type},#{item.uri},#{item.request}," +
            "#{item.response},#{item.returnCode},DATE_FORMAT(#{item.occurredTime},'%Y-%m-%d %H:%i:%s'))" +
            "</foreach> </script>"})
    void recordNotes(@Param("simulateNotes") List<SimulateNotes> simulateNotes);


    @Delete({"<script> DELETE FROM material " +
            "<if test='all!=true'> " +
            "WHERE id in" +
            "<foreach collection=\"ids\" item=\"id\" index=\"no\"  open=\"(\" separator=\",\" close=\")\" >" +
            "#{id}" +
            "</foreach>" +
            "</if> " +
            "</script>"})
    Integer deleteMaterial(@Param("all") boolean all, @Param("ids") List<String> ids);
}
